using System;
using System.Windows;
using System.Windows.Input;

namespace Application
{
    public class WindowAdapter : IWindow
    {
        private readonly Window _wpfWindow;

        public WindowAdapter(Window window)
        {
            if (window == null) throw new ArgumentNullException("window");

            _wpfWindow = window;
        }

        public virtual void Close()
        {
            _wpfWindow.Close();
        }

        public virtual IWindow CreateChild(object viewModel)
        {
            var contentWindow = new ContentWindow
                                 {
                                     Owner = _wpfWindow, 
                                     DataContext = viewModel,
                                     WindowStartupLocation = WindowStartupLocation.Manual,Content = new EditorControl()
                                 };
            contentWindow.CommandBindings.Add(new CommandBinding(PresentationCommands.Accept, (sender, e) => contentWindow.DialogResult = true));

            return new WindowAdapter(contentWindow);
        }

        public virtual void Show()
        {
            _wpfWindow.Show();
        }

        public virtual bool? ShowDialog()
        {
            return _wpfWindow.ShowDialog();
        }

        protected Window WpfWindow
        {
            get { return _wpfWindow; }
        }
    }

    public static class PresentationCommands
    {
        private readonly static RoutedCommand _accept = new RoutedCommand("Accept", typeof(PresentationCommands));

        public static RoutedCommand Accept
        {
            get { return _accept; }
        }
    }
}